/*
 * Copyright (c) 2010 Curtis Boyden.  All rights reserved. 
 * This software was developed by Curtis Boyden and is provided under the terms 
 * of the GNU Lesser General Public License, Version 2.1. You may not use 
 * this file except in compliance with the license. If you need a copy of the license, 
 * please go to http://www.gnu.org/licenses/lgpl-2.1.txt. The Original Code is ejMud
 * (Ewerp Java MUD).  The Initial Developer is Curtis Boyden.
 *
 * Software distributed under the GNU Lesser Public License is distributed on an "AS IS" 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to 
 * the license for the specific language governing your rights and limitations.
*/

package com.ewerp.mud.entities;

import com.ewerp.mud.messages.IMessage;

public abstract class BaseEntity implements IEntity {
    protected IObjectId objectId;
    
    public abstract void processMessage(IMessage message);
    
    @Override
    public IObjectId getObjectId() {
        if(objectId == null) {
            objectId = new ObjectId();
        }
        
        if(objectId instanceof ObjectId) {
            ObjectId objId = (ObjectId) objectId;
            
            if(objId.getObjectClass() == null) {
                objId.setObjectClass(getClass().getName());
            }
            if(objId.getObject() == null) {
                objId.setObject(this);
            }
        }
        
        return objectId;
    }

    // Only used by Hibernate
    // Do not access directly
    public void setObjectId(IObjectId id) {
        objectId = id;
//        if(objectId == null) {
//            if(id instanceof ObjectId) {
//                objectId = (ObjectId)id;
//            }
//            else {
//                Issue here with Hibernate. We are replacing their object
//                Consider wrapping it
//                objectId = new ObjectId();
//                objectId.setObjectClass(getClass().getName());
//                objectId.setObject(this);
//                objectId.setSerial(id.getSerial());
//            }
//        }
    }

    @Override
    public boolean equals(Object obj) {
        if(obj != null && !(obj instanceof BaseEntity)) {
            return false;
        }
        
        return getObjectId() == null ? false : getObjectId().equals(((BaseEntity)obj).getObjectId());
    }

    @Override
    public int hashCode() {
        return getObjectId() == null ? Integer.MIN_VALUE : getObjectId().hashCode();
    }

    @Override
    public String toString() {
        return getClass().getName() + ": " + getObjectId() == null ? "Unassigned" : getObjectId().toString();
    }
    
    /**
     * Required by HIBERNATE: Do not use!
     * @return
     */
    public long getSerial() {
        // If the IObjectId does not support serial, then we can ignore it
        if(getObjectId() instanceof ObjectId) {
            return ((ObjectId)getObjectId()).getSerial();
        }
        return -1;
    }
    
    /**
     * Required by HIBERNATE: Do not use!
     * @param serial
     */
    public void setSerial(long serial) {
        // If the IObjectId does not support serial, then we can ignore it
        if(getObjectId() instanceof ObjectId) {
            ((ObjectId)getObjectId()).setSerial(serial);
        }
    }
}
